package com.example.rediscache.service.impl;

import com.example.rediscache.redis.RedisLock;
import com.example.rediscache.service.SecKillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created By JYFeng
 * 2018/11/22
 */
@Service
public class SecKillServiceImpl implements SecKillService {

    private static final int TIMEOUT = 10 * 1000; //超时时间 10s

    @Autowired
    private RedisLock redisLock;

    /**
     * 国庆活动，皮蛋粥特价，限量100000份
     */
    static Map<String,Integer> products;
    static Map<String,Integer> stock;
    static Map<String,String> orders;
    static {
        /**
         * 模拟多个表，商品信息表，库存表，秒杀成功订单表
         */
        products = new HashMap<>();
        stock = new HashMap<>();
        orders = new HashMap<>();
        products.put("123456", 100);
        stock.put("123456", 100);
    }

    private String queryMap(String productId) {
        return "国庆活动，皮蛋粥特价，限量份"
                + products.get(productId)
                +" 还剩：" + stock.get(productId)+" 份"
                +" 该商品成功下单用户数目："
                +  orders.size() +" 人" ;
    }

    @Override
    public String querySecKillProductInfo(String productId) {
        return this.queryMap(productId);
    }

    @Override
    public void orderProductMockDiffUser(String productId) throws Exception{

        long currentTime = System.currentTimeMillis() + TIMEOUT;

        // 加锁
        if (!redisLock.lock(productId, String.valueOf(currentTime))) {
            throw new Exception("人太多了，骚等再试~~~");
        }

        // 1.查询该商品库存，为0则活动结束。
        int stockNum = stock.get(productId);

        if(stockNum == 0) {
            throw new Exception("活动结束");
        }else {

            // 2.下单(模拟不同用户openid不同)
            String uniqueKey = UUID.randomUUID().toString().replace("-", "");
            orders.put(uniqueKey, productId);
//            orders.put(KeyUtil.genUniqueKey(),productId);

            //3.减库存
            stockNum = stockNum - 1;
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            stock.put(productId, stockNum);
        }

        // 解锁
        redisLock.unlock(productId, String.valueOf(currentTime));
    }
}
